package penguin.masking;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class FileWordManager extends WordManager {

    @Override
    protected SensitiveWordMasking loadDefaultMasking(SensitiveWordMasking defaultMasking) {
        String keysPath = MaskingContext.context().getKeysPath();
        if (keysPath == null) {
            return null;
        }
        final File f = new File(keysPath);
        if (!f.exists() || !f.isFile() || f.isHidden()) {
            return null;
        }
        if (defaultMasking == null || f.lastModified() != defaultMasking.getLastModifiedTime()) {
            return scanFileWord(f);
        }
        return null;
    }

    private SensitiveWordMasking scanFileWord(File f) {
        try (BufferedReader is = new BufferedReader(new FileReader(f))) {
            String line = null;
            Set<String> lineSet = new HashSet<>();
            while ((line = is.readLine()) != null) {
                lineSet.add(line);
            }
            SensitiveWordMasking masking = new SensitiveWordMasking(lineSet);
            masking.setLastModifiedTime(f.lastModified());
            return masking;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected Map<String, SensitiveWordMasking> reloadUserMasking(Map<String, SensitiveWordMasking> userMaskingMap) {
        String keysPath = MaskingContext.context().getKeysPath();
        if (keysPath == null) {
            return null;
        }
        final File f = new File(keysPath);
        final File parentFile = f.getParentFile();
        File[] files = parentFile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                if (file.isHidden() || file.isDirectory()) {
                    return false;
                }
                String name = file.getName();
                return !name.equals(f.getName())
                        &&
                        name.substring(name.lastIndexOf(".")).equals(f.getName().substring(f.getName().lastIndexOf(".")))
                        &&
                        name.startsWith(f.getName().substring(0, f.getName().lastIndexOf(".")));
            }
        });
        Map<String, SensitiveWordMasking> _maskingMap = new HashMap<>();
        for (File file : files) {
            if (!file.exists() || !file.isFile() || file.isHidden()) {
                continue;
            }
            String name = userFromFile(file);
            SensitiveWordMasking wordMasking = userMaskingMap.get(name);
            if (wordMasking == null || file.lastModified() != wordMasking.getLastModifiedTime()) {
                SensitiveWordMasking masking = scanFileWord(file);
                masking.setUser(name);
                _maskingMap.put(masking.getUser(), masking);
            } else {
                _maskingMap.put(wordMasking.getUser(), wordMasking);
            }
        }
        return _maskingMap;
    }

    private String userFromFile(File file) {
        return file.getName().substring(file.getName().indexOf("-") + 1, file.getName().lastIndexOf("."));
    }
}
